home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PROGEDIT / 0748.ZIP / COMMENTS < prev    next >
Text File  |  1987-01-26  |  3KB  |  108 lines

  1. /********************************************************************/
  2. /* COMMENTS - macro for the ME text editor to handle comments       */
  3. /*              when programming in C.                              */
  4. /*                                                                  */
  5. /*   Written 1/23/87 by Marc Adler of Magma Software Systems        */
  6. /*                                    138 - 23 Hoover Ave.          */
  7. /*                                    Jamaica, NY  11435            */
  8. /*                                                                  */
  9. /********************************************************************/
  10.  
  11.  
  12. #define ALT_A   158
  13. #define ALT_C   174
  14.  
  15. int  CommentColumn;
  16.  
  17. init()
  18. {
  19.   CommentColumn = 55;
  20.   assign_key("align_file", ALT_A);
  21.   assign_key("do_comment", ALT_C);
  22. }
  23.  
  24. /* align_file - aligns all comments in a file */
  25. align_file()
  26. {
  27.   int  lastln, i;
  28.   int  startcol;
  29.  
  30.   lastln = lastlinenum();
  31.   gobof();
  32.  
  33.   for (i = 1;  i <= lastln;  i = i + 1)
  34.   {
  35.     if ((startcol = index(currline(), "/*")) > 1  &&  /* has a cmnt already */
  36.       /* If the comment is the only thing on the line, then don't touch it. */
  37.         !is_line_blank(substr(currline(), 1, startcol - 1)))
  38.       do_comment();
  39.     gobol();
  40.     down();
  41.   }
  42. }
  43.  
  44. /* do_comment - inserts a comment if none already in a line */
  45. /*              or else aligns the comment at CommentColumn */
  46. do_comment()
  47. {
  48.   string cl;
  49.   int    startcol, textcol;
  50.  
  51.   if ((startcol = index(currline(), "/*")) > 0)     /* has a cmnt already */
  52.   {
  53.     setcol(startcol);           /* move to the comment */
  54.     if (startcol < CommentColumn)
  55.       /* slide the comment over till it hits the comment column */
  56.       insert(repstr(" ", CommentColumn - startcol));
  57.     else
  58.     {
  59.       /* slide the comment leftwards */
  60.       save_position();
  61.       /* move the cursor left until we reach a non-blank column */
  62.       left();
  63.       while (currchar() == ' ' && !isbol())
  64.         left();
  65.       textcol = currcol();      /* save the column # of the last char */
  66.       restore_position();
  67.       
  68.       /* Delete leftwards until we reach the starting column */
  69.       while (startcol > textcol + 3 && startcol > CurrentColumn)
  70.       {
  71.         backspace();
  72.         startcol--;
  73.       }
  74.     } /* if startcol < CommentCol */
  75.   } /* if startcol = index() */
  76.  
  77.   else                  /* No comment in the line yet, so insert one */
  78.   {
  79.     cl = currline();               /* make a copy of the current line*/
  80.     if (substr(cl, strlen(cl), 1) == " ")
  81.     {
  82.       rtrim(cl);                   /* get rid of the trailing blanks */
  83.       gobol();                     /*  on the current line           */
  84.       deleol();
  85.       insert(cl);
  86.     }
  87.     
  88.     if (currcol() < CommentColumn) /* move to the comment column */
  89.       setcol(CommentColumn);
  90.     insert("/*  */");              /* put the comment at the end of the ln */
  91.     left();  left();  left();      /* move to just after the open comment  */
  92.   }
  93. }
  94.  
  95. is_line_blank(str)
  96.   string str;
  97. {
  98.   int i;
  99.   int len;
  100.  
  101.   len = strlen(str);
  102.  
  103.   for (i = 1;  i <= len;  i++)
  104.     if (substr(str, i, 1) != " ")
  105.       return 0;
  106.   return 1;
  107. }
  108.